package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.*;
import cn.wolfcode.wolf2world.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2world.query.StrategyQueryObject;
import cn.wolfcode.wolf2world.redis.service.StaticRedisService;
import cn.wolfcode.wolf2world.redis.service.UserRedisService;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.StrategyCommentService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.service.StrategyThemeService;
import cn.wolfcode.wolf2world.vo.JsonResult;
import cn.wolfcode.wolf2world.vo.StaticRedisVO;
import cn.wolfcode.wolf2world.web.annotation.RequiredLogin;
import cn.wolfcode.wolf2world.web.annotation.UserParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020-08-12 22:51
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private StrategyService strategyService;
    @Autowired
    private StrategyThemeService strategyThemeService;
    @Autowired
    private DestinationService destinationService;
    @Autowired
    private StrategyCommentService strategyCommentService;
    @Autowired
    private StaticRedisService staticRedisService;
    @Autowired
    private UserRedisService userRedisService;

    /**
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id) {
        Strategy strategy = strategyService.get(id);


        //阅读数+1
        staticRedisService.addViewnum(id);

        return JsonResult.success(strategy);
    }

    @RequestMapping("/themes")
    public Object themes(String destId) {
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }
    @RequestMapping("/query")
    public Object query(StrategyQueryObject qo) {
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    @RequestMapping("/toasts")
    public Object toasts(String destId) {
        List<Destination> toasts = destinationService.toasts(destId);
        return JsonResult.success(toasts);
    }

    /**
     * 新增一条攻略的评论
     * @param userInfo
     * @param comment
     * @return
     */
    @RequiredLogin
    @RequestMapping("/addComment")
    public Object addComment(@UserParam UserInfo userInfo, StrategyComment comment) {
        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        comment.setCreateTime(new Date());
        strategyCommentService.insert(comment);

        //评论数+1
        staticRedisService.addComment(comment.getStrategyId());
        return JsonResult.success();
    }

    /**
     * 高级查询攻略的评论
     * @param qo
     * @return
     */
    @RequestMapping("/comments")
    public Object comments(StrategyCommentQueryObject qo) {
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 用户点击评论的赞
     * @param userInfo
     * @param cid
     * @return
     */
    @RequiredLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(@UserParam UserInfo userInfo,String cid) {
        //通过评论id获取评论对象
        StrategyComment comment = strategyCommentService.get(cid);
        //再获取对象的点赞过的用户id集合
        List<String> thumbUpList = comment.getThumbUpList();
        //判断当前用户是否存在于这个点赞id集合中
        if (thumbUpList.contains(userInfo.getId())) {
            //存在，则是取消点赞，移除当前用户id
            thumbUpList.remove(userInfo.getId());
            //点赞数-1
            comment.setThumbUpNum(comment.getThumbUpNum() - 1);
        }else {
            //不存在，则是点赞，添加当前用户id到集合列表
            thumbUpList.add(userInfo.getId());
            //点赞数 +1
            comment.setThumbUpNum((comment.getThumbUpNum() == null ? 0 : comment.getThumbUpNum())+ 1);
        }
        //保存
        strategyCommentService.update(comment);
        return JsonResult.success();
    }


    /**
     * 获取数据统计
     * @param sid
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        StaticRedisVO vo = staticRedisService.getStaticRedisVO(sid);

        return JsonResult.success(vo);
    }

    /**
     * 点击收藏
     * @param sid
     * @return
     */
    @RequiredLogin
    @PostMapping("/favor")
    public Object favor(@UserParam UserInfo userInfo, String sid) {
        boolean ret = userRedisService.clickFavor(userInfo.getId(),sid);
        return JsonResult.success(ret);
    }

    /**
     * 顶操作
     * @param userInfo
     * @param sid
     * @return
     */
    @RequiredLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(@UserParam UserInfo userInfo, String sid) {
        boolean ret = staticRedisService.clickThumbup(userInfo.getId(),sid);
        return JsonResult.success(ret);
    }


}
